/**
 * 会员活动
 * @constructor
 */
function MemberEvent() {
}

MemberEvent.init = function () {
    //日期区间选择
    laydate.render({
        elem: '#text_CreateTime',
        theme: 'molv',
        type: 'date',
    });
    laydate.render({
        elem: '#text_couponStarTime',
        theme: 'molv',
        type:"datetime",
    });
    laydate.render({
        elem: '#text_couponEndTime',
        theme: 'molv',
        type:"datetime",
    });
    bindServselect();
    $('#tableList').bootstrapTable({
        url: '/event/tableSearch',
        method: 'post',
        striped : true, //是否显示行间隔色
        toolbar: "#toolbar",
        sidePagination: "true",
        dataField: "data",//这是返回的json数组的key.默认是"rows".这里只有前后端约定好就行
        showToggle : true, //是否显示详细视图和列表视图的切换按钮
        striped: true, // 是否显示行间隔色
        //search : "true",
        uniqueId: "id",
        pageList : [ 5, 10, 20,50,100],//可选择单页记录数
        pageSize: "10",
        pagination: true, // 是否分页
        sortable: true, // 是否启用排序
        sidePagination: 'server', // 设置为服务器端分页     客户端：client
        search:false,
        showColumns: true,
        showRefresh: true,
        //得到查询的参数
        queryParams: function (params) {
            //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            var query_params = {
                //********table的参数 不变
                pageSize: params.limit,                         //页面大小
                pageIndex: (params.offset / params.limit) + 1,   //页码
                sort: params.sort,      //排序列名
                sortOrder: params.order, //排位命令（desc，asc）
                //********
                //获取查询的对象，往下加入
                userName: $('#search_Name').val(), // 请求时向服务端传递的参数
                phone: $('#search_Phone').val(),
            };
            return query_params;
        },
        columns: [
            {
                //field: 'Number',//可不加
                title: '序号',//标题  可不加
                align: "center",
                width: 40,
                formatter: function (value, row, index) {
                    var pageSize=$('#tableList').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                    var pageNumber=$('#tableList').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                    return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                }
            },
            { field: 'userName', title: '会员名称',align: 'center'},
            { field: 'createDateTime', title: '加盟时间',align: 'center'},
            { field: 'phone', title: '联系电话',align: 'center' },
            {
                title: '余额',
                align: 'center',
                formatter: function (value, row, index) {
                    var totalamount =row.amount+row.frozenAmount;
                    return totalamount;
                }
            },
            {
                title: '操作',
                align: 'center',
                width: '25%',
                formatter: function (value, row, index) {
                    var optionHtml = "";
                    optionHtml = optionHtml+" <a class=\"btn btn-success btn-xs\" href=\"javascript:void(0)\" onclick='actionRecord(" + JSON.stringify(row) + ",\"coupon\")' title=\"添加优惠券\" ><i class=\"fa fa-edit\"></i>添加优惠券</a>&nbsp";
                    optionHtml = optionHtml+" <a class=\"btn btn-success btn-xs\" href=\"javascript:void(0)\" onclick='actionRecord(" + JSON.stringify(row) + ",\"add\")' title=\"充值\" ><i class=\"fa fa-edit\"></i>充值</a>&nbsp";
                    optionHtml = optionHtml+" <a class=\"btn btn-danger btn-xs\"  href=\"javascript:void(0)\" onclick='actionRecord(" + JSON.stringify(row) + ",\"del\")'  title=\"扣款\"><i class=\"fa fa-remove\"></i>扣款</a>&nbsp";
                    optionHtml = optionHtml+" <a class=\"btn btn-success btn-xs\" href=\"javascript:void(0)\" onclick='actionRecord(" + JSON.stringify(row) + ",\"event\")'  title=\"参与活动\"><i class=\"fa fa-shopping-cart\"></i>参与活动</a>";
                    return optionHtml;

                }
            }
        ]
    });
}
//查询方法****************************
function doSearch() {
    $('#tableList').bootstrapTable(('refresh')); // 很重要的一步，刷新url！
}
//重置****************************
function doRest() {
    $("#search_Name").val("");
    $("#search_Phone").val("");
    $('#tableList').bootstrapTable(('refresh')); // 很重要的一步，刷新url！
}

//操作相关方法****************************
function actionRecord(data,type) {
    if(type=="add"){
        //充值
        userID=data.userID;
        $('#myModalAdd').modal('show');
    }else if(type=="del"){
        //扣款
        userID=data.userID;
        $('#myModalDel').modal('show');
    }else if(type=="event"){
        //扣款
        userID=data.userID;
        $('#myModalEvent').modal('show');
    }else if(type=="coupon"){
        //添加优惠券
        userID=data.userID;
        $('#myModalCoupon').modal('show');
    }
}

//给控件赋值****************************
function addAmount() {
    var amount=$("#add_Amount").val();
    var data = {
        amount: amount,
        userID: userID,
    };
    //充值
    swal({
        title: "确认是否充值",
        text: "是否确认要充值"+amount+"?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "充值",
        cancelButtonText: "取消",
        closeOnConfirm: false
    }, function () {
        //点击充值之后此处进行传递后台
        $.ajax({
            url:"/event/addAmount",
            type:"post",
            data:JSON.stringify(data),
            contentType:"application/json",
            dataType:"json",
            success:function(data){
                if (data.code === "200") {
                    $('#myModalAdd').modal('hide')
                    $('#tableList').bootstrapTable(('refresh'));
                    swal("充值成功!", "请继续操作", "success");
                } else {
                    swal("充值失败!", "请重试", "error");
                }
            }
        })
    });
}
function delAmount() {
    var amount=$("#del_Amount").val();
    var data = {
        amount: amount,
        userID: userID,
    };
    //扣款
    swal({
        title: "确认是否扣款",
        text: "是否确认要扣款"+amount+"?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "扣款",
        cancelButtonText: "取消",
        closeOnConfirm: false
    }, function () {
        //点击充值之后此处进行传递后台
        $.ajax({
            url:"/event/delAmount",
            type:"post",
            data:JSON.stringify(data),
            contentType:"application/json",
            dataType:"json",
            success:function(data){
                if (data.code === "200") {
                    $('#myModalDel').modal('hide')
                    $('#tableList').bootstrapTable(('refresh'));
                    swal("扣款成功!", "请继续操作", "success");
                } else {
                    swal("扣款失败!", "请重试", "error");
                }
            }
        })
    });
}
//参与活动
function actEvent() {
    var eventId=$("#text_Event").val();
    var data = {
        id: eventId,
        userID: userID,
    };
    //参与活动
    swal({
        title: "确认是否参与活动",
        text: "是否确认要参与活动?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "参与",
        cancelButtonText: "取消",
        closeOnConfirm: false
    }, function () {
        //点击充值之后此处进行传递后台
        $.ajax({
            url:"/event/actEvent",
            type:"post",
            data:JSON.stringify(data),
            contentType:"application/json",
            dataType:"json",
            success:function(data){
                if (data.code === "200") {
                    $('#myModalEvent').modal('hide')
                    $('#tableList').bootstrapTable(('refresh'));
                    swal("参与活动成功!", "请继续操作", "success");
                } else {
                    swal("参与活动失败!", "请重试", "error");
                }
            }
        })
    });
}
//添加优惠券
function addCoupon() {
    var couponName=$("#text_Coupon").val();
    var money=$("#text_Money").val();
    var amount=$("#text_Amount").val();
    var startTime=$("#text_couponStarTime").val();
    var endTime=$("#text_couponEndTime").val();
    var data = {
        userId: userID,
        couponName: couponName,
        money: money,
        amount: amount,
        startTime: startTime,
        endTime: endTime,
    };
    //添加优惠券
    swal({
        title: "确认是否添加优惠券",
        text: "是否确认要添加优惠券?",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "添加",
        cancelButtonText: "取消",
        closeOnConfirm: false
    }, function () {
        //点击添加之后此处进行传递后台
        $.ajax({
            url:"/event/addCoupon",
            type:"post",
            data:JSON.stringify(data),
            contentType:"application/json",
            dataType:"json",
            success:function(data){
                if (data.code === "200") {
                    $('#myModalCoupon').modal('hide')
                    $('#tableList').bootstrapTable(('refresh'));
                    swal("添加优惠券成功!", "请继续操作", "success");
                } else {
                    swal("添加优惠券失败!", "请重试", "error");
                }
            }
        })
    });
}
function bindServselect() {
    $.ajax({
        url: "/event/getEvent",
        type: "get",
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            var ddl1 = $("#text_Event");
            //删除节点
            $("#text_Event option").remove();
            //转成Json对象
            var result = eval(data.data);
            var opt1 = $("<option value=''></option>").text("--请选择参与活动--").val("");
            ddl1.append(opt1);
            //循环遍历 下拉框绑定
            $(result).each(function (key) {
                //添加option 对应Json对象名称
                opt1 = $("<option></option>").text(result[key].eventName+"---"+result[key].eventRemark).val(result[key].id);
                ddl1.append(opt1);
            });
        },
        error: function () {
            // alert("Error");
        }
    });
}
//*****************************************************************************************
